﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

using PowerCreator.LMS.Common.Script;
using PowerCreator.LMS.Web.DL.Project;
using PowerCreator.LMS.DL.Project.Entity;
using PowerCreator.LMS.Web.Catalog;
using PowerCreator.LMS.Web.Course;
using PowerCreator.LMS.Course.Entity;

public partial class sysadmin_DL_Project_AddCourseList : PowerCreator.LMS.Web.Base.WebBasePage
{
    TOCourseList objCourseList = new TOCourseList();
    VOTOCourseType voCourseType = new VOTOCourseType();
    TOCourseType objCourseType = new TOCourseType();
    VOTOPlans voPlans = new VOTOPlans();
    TOPlans objPlans = new TOPlans();
    private PowerCreator.LMS.Web.DL.User.LoginEntity login = new PowerCreator.LMS.Web.DL.User.LoginEntity();
    private string url = "../../login.aspx";

    protected void Page_Load(object sender, EventArgs e)
    {
        //if (IsLicensed(PowerCreator.LMS.Action.Core.ActionInfo.AddJob))
        //{
        //    Script.RedirectURL(GetRefUrl());
        //}
        CheckUser();
        if (!IsPostBack)
        {
            InitPage();
            BinderBatch();
            //BinderPlan(int.Parse(ddl_Batch.SelectedValue));
            
            BinderCourseType();
            BinderSchoolStage(int.Parse(ddl_CourseType.SelectedValue));
            BinderCatalog(int.Parse(ddl_CourseType.SelectedValue));
            BinderRpt(int.Parse(ddl_CourseType.SelectedValue));

            int CLID = 0;
            if (Request.QueryString["CLID"] != null)
            {
                CLID = int.Parse(Request.QueryString["CLID"].ToString());
                ViewState["CLID"] = CLID.ToString();
                BinderForm(CLID);
            }
            BinderBatch_shifen(int.Parse(ddl_Batch.SelectedValue));
        }
    }

    private void CheckUser()
    {
        if (Session[PowerCreator.LMS.Web.Config.WebConfig.SESSION_USER_USERINFO] == null)
        {
            Script.RedirectNewUrl(url);
        }
        else
        {
            login = (PowerCreator.LMS.Web.DL.User.LoginEntity)Session[PowerCreator.LMS.Web.Config.WebConfig.SESSION_USER_USERINFO];
            if (login == null)
            {
                Script.RedirectNewUrl(url);
            }
            else
            {
                if (login.VOAdvancedUser == null)
                {
                    Script.RedirectNewUrl(url);
                }
                else if (login.VOAdvancedUser.AUserGroup > 3)
                {
                    //Response.Write(login.VOAdvancedUser.AUserGroup.ToString());
                    //Response.End();
                    Script.ShowAndHistory("您无此权限！", -1);
                }
            }
        }


    }
    protected override void InitPage()
    {
        //lbl_InfoTitle.Text = GetLanageValueByKey("lbl_AddJob_InfoTitle", PowerCreator.LMS.Language.LanagetItemType.Page);
        //lbl_JobName.Text = GetLanageValueByKey("lbl_AddJob_JobName", PowerCreator.LMS.Language.LanagetItemType.Page);
        //lbl_JobDescription.Text = GetLanageValueByKey("lbl_AddJob_JobDescription", PowerCreator.LMS.Language.LanagetItemType.Page);

        //btnSave.Text = GetLanageValueByKey("btn_Save", PowerCreator.LMS.Language.LanagetItemType.Page);
        //btnReset.Text = GetLanageValueByKey("btn_Reset", PowerCreator.LMS.Language.LanagetItemType.Page);
        //base.InitPage();

        //lbl_InfoTitle_AddNewType.Text = GetLanageValueByKey("lab_AddNewType_InfoTitle", PowerCreator.LMS.Language.LanagetItemType.Page);
        //lbl_NewTypeName.Text = GetLanageValueByKey("lab_AddNewType_TypeName", PowerCreator.LMS.Language.LanagetItemType.Page);

        btnSave.Text = GetLanageValueByKey("btn_Save", PowerCreator.LMS.Language.LanagetItemType.Page);
        btnReset.Text = GetLanageValueByKey("btn_Reset", PowerCreator.LMS.Language.LanagetItemType.Page);
        base.InitPage();
    }


    //绑定批次
    private void BinderBatch()
    {
        TOBatch obj = new TOBatch();
        string search = " Used=1";
        DataTable dt = obj.Query(search);

        ddl_Batch.DataSource = dt;
        ddl_Batch.DataTextField = "Batch";
        ddl_Batch.DataValueField = "Batch_Id";
        ddl_Batch.DataBind();

    }


    ////绑定教学计划
    //private void BinderPlan(int BatchID)
    //{
    //    TOPlans obj = new TOPlans();
    //    string search = " Used=1";
    //    if (BatchID != 0)
    //        search += " and BatchID=" + BatchID.ToString();
    //    DataTable dt = obj.Query(search);

    //    ddl_Plans.DataSource = dt;
    //    ddl_Plans.DataTextField = "PlansName";
    //    ddl_Plans.DataValueField = "Plans_Id";
    //    ddl_Plans.DataBind();

    //}


    //绑定批次的学时和学分
    private void BinderBatch_shifen(int BatchID)
    {
        TOBatch obj = new TOBatch();
        VOTOBatch opTobacth = new VOTOBatch();
        opTobacth = obj.Load(BatchID);

        lbl_Batch_xueshi.Text = opTobacth.Batch_xueshi.ToString().Trim();
        lbl_Batch_xuefen.Text = opTobacth.Batch_xuefen.ToString().Trim();
    }


    //绑定课程资源分类
    private void BinderCourseType()
    {
        TOCourseType obj = new TOCourseType();
        string search = " Used=1";
        DataTable dt = obj.Query(search);

        ddl_CourseType.DataSource = dt;
        ddl_CourseType.DataTextField = "CourseTypeName";
        ddl_CourseType.DataValueField = "CourseType_Id";
        ddl_CourseType.DataBind();

    }



    //绑定学段
    private void BinderSchoolStage(int CourseType_Id)
    {
        TOSchoolStage obj = new TOSchoolStage();
        string search = "";
        DataTable dt = obj.Query(search);

        ddl_XueDuan.DataSource = dt;
        ddl_XueDuan.DataTextField = "SchoolStage";
        ddl_XueDuan.DataValueField = "ID";
        ddl_XueDuan.DataBind();

        voCourseType = objCourseType.Load(CourseType_Id);
        
        ddl_XueDuan.SelectedValue = voCourseType.Xueduan.ToString();
        
        ddl_XueDuan.Enabled = false;

    }

    //绑定学科
    private void BinderCatalog(int CourseType_Id)
    {
        Catalog obj = new Catalog();
        string search = "";
        DataTable dt = obj.QueryCatalog(search);

        ddl_XueKe.DataSource = dt;
        ddl_XueKe.DataTextField = "CatalogName";
        ddl_XueKe.DataValueField = "CatalogId";
        ddl_XueKe.DataBind();

        voCourseType = objCourseType.Load(CourseType_Id);
        ddl_XueKe.SelectedValue = voCourseType.Xueke.ToString();
        ddl_XueKe.Enabled = false;

    }

    private void BinderRpt(int coursetype_id)
    {
        Course objCourse = new Course();
        //VOTOCourseType vo = new VOTOCourseType();
        //TOCourseType obj = new TOCourseType();
        voCourseType = objCourseType.Load(coursetype_id);
        string CourseIDsList = "0," + voCourseType.CourseID;

        string sql = " Course_Id in (" + CourseIDsList + ") and CourseIsCompulsory=1";
        DataTable dt = objCourse.QueryCourse(sql);

        rptCourselist.DataSource = dt;
        rptCourselist.DataBind();

    }

    private string getCourseIDlist()
    {
        //string CourseIDlist = string.Empty;
        string CourseIDlist = "";
        // CheckBoxList chkList = (CheckBoxList)rptTrainClassIDlist.Items[i].FindControl("chk_FKNewTrainClaIDlist");
        for (int i = 0; i < rptCourselist.Items.Count; i++)
        {
            CheckBox chk = (CheckBox)rptCourselist.Items[i].FindControl("chk_CourseID");
            Label lbl_CourseID = (Label)rptCourselist.Items[i].FindControl("lbl_CourseID");
            TextBox txt_Score = (TextBox)rptCourselist.Items[i].FindControl("txt_Score");
            //TextBox txt_Score = (TextBox)rptCourselist.Items[i].FindControl("txt_Score");
            //TextBox txt_studyTime = (TextBox)rptCourselist.Items[i].FindControl("txt_studyTime");
            //Label lbl_Score = (Label)rptCourselist.Items[i].FindControl("lbl_Score");
            //Label lbl_studyTime = (Label)rptCourselist.Items[i].FindControl("lbl_studyTime");
            //RadioButton radbtn_bixiu = (RadioButton)rptCourselist.Items[i].FindControl("radbtn_bixiu");
            //RadioButton radbtn_xuanxiu = (RadioButton)rptCourselist.Items[i].FindControl("radbtn_xuanxiu");
            if (chk.Checked)
            {
                if (CourseIDlist.Length > 0)
                    CourseIDlist += "," + lbl_CourseID.Text+"#"+txt_Score.Text;
                else
                    CourseIDlist =  lbl_CourseID.Text + "#" + txt_Score.Text;
                ////if(CourseIDlist.Length>0)
                ////    CourseIDlist+="#";
                ////CourseIDlist += lbl_CourseID.Text + ",";
                //CourseIDlist += txt_Score.Text + ",";
                //CourseIDlist += txt_studyTime.Text + ",";
                ////CourseIDlist += "0,";
                ////CourseIDlist += "0,";
                //if (radbtn_bixiu.Checked)
                //    CourseIDlist += "1";
                //else if(radbtn_xuanxiu.Checked)
                //    CourseIDlist += "0";
                ////CourseIDlist += "1";             
            
            }

        }
        return CourseIDlist;
    }


    private void BinderForm(int id)
    {
        VOTOCourseList vo = new VOTOCourseList();
        vo = objCourseList.Load(id);

        txt_Name.Text = vo.CourseListName;
        txt_Introduce.Text = vo.Description;
        ddl_Batch.SelectedValue = vo.Batch_Id.ToString();
        //ddl_Plans.SelectedValue = vo.Plans_Id.ToString();
        ddl_CourseType.SelectedValue = vo.CourseType_Id.ToString();
        ddl_XueDuan.SelectedValue = vo.Xueduan.ToString();
        ddl_XueKe.SelectedValue = vo.Xueke.ToString();
        if (vo.Used)
        {
            radbtn_Yes.Checked = true;
            radbtn_No.Checked = false;
        }
        else
        {
            radbtn_Yes.Checked = false;
            radbtn_No.Checked = true;
        }

        BinderRpt(vo.CourseType_Id);
        BandingCourselist(vo.CourseID);
        BinderBatch_shifen(int.Parse(ddl_Batch.SelectedValue));
        //Course obj = new Course();
        //VOTOCourse opcourse = new VOTOCourse();
        //string[] CourseIDsList = vo.CourseID.Split('#'); 
        //for (int i = 0; i < rptCourselist.Items.Count; i++)
        //{
        //    CheckBox chk = (CheckBox)rptCourselist.Items[i].FindControl("chk_CourseID");
        //    Label lbl_CourseID = (Label)rptCourselist.Items[i].FindControl("lbl_CourseID");
        //    //TextBox txt_Score = (TextBox)rptCourselist.Items[i].FindControl("txt_Score");
        //    //TextBox txt_studyTime = (TextBox)rptCourselist.Items[i].FindControl("txt_studyTime");
        //    Label lbl_Score = (Label)rptCourselist.Items[i].FindControl("lbl_Score");
        //    Label lbl_studyTime = (Label)rptCourselist.Items[i].FindControl("lbl_studyTime");
        //    //RadioButton radbtn_bixiu = (RadioButton)rptCourselist.Items[i].FindControl("radbtn_bixiu");
        //    //RadioButton radbtn_xuanxiu = (RadioButton)rptCourselist.Items[i].FindControl("radbtn_xuanxiu");

        //    for (int j = 0; j < CourseIDsList.Length; j++)
        //    {
        //        string[] parameters = CourseIDsList[j].Split(',');
        //        if (parameters.Length == 4)
        //        {
        //            if (parameters[0] == lbl_CourseID.Text)
        //            {
        //                chk.Checked = true;                        
        //                //txt_Score.Text = parameters[1].ToString();
        //                //txt_studyTime.Text = parameters[2].ToString();
        //                //if (parameters[3].ToString() == "1")
        //                //    radbtn_bixiu.Checked = true;
        //                //else if (parameters[3].ToString() == "0")
        //                //    radbtn_xuanxiu.Checked = true;
        //            }
        //        }
        //        opcourse = obj.LoadVOTCourse(int.Parse(lbl_CourseID.Text));
        //        lbl_Score.Text = opcourse.Score.ToString().Trim();
        //        lbl_studyTime.Text = opcourse.CoursePeriod.ToString().Trim();
        //    }
            

        //}
    }

    private void BandingCourselist(string CourseIDList)
    {
        Course obj = new Course();
        VOTOCourse opcourse = new VOTOCourse();
        //string[] CourseIDsList = CourseIDList.Split('#');
        string[] CourseIDsList = CourseIDList.Split(',');
        for (int i = 0; i < rptCourselist.Items.Count; i++)
        {
            CheckBox chk = (CheckBox)rptCourselist.Items[i].FindControl("chk_CourseID");
            Label lbl_CourseID = (Label)rptCourselist.Items[i].FindControl("lbl_CourseID");
            TextBox txt_Score = (TextBox)rptCourselist.Items[i].FindControl("txt_Score");
            //Label lbl_Score = (Label)rptCourselist.Items[i].FindControl("lbl_Score");
            //Label lbl_studyTime = (Label)rptCourselist.Items[i].FindControl("lbl_studyTime");

            for (int j = 0; j < CourseIDsList.Length; j++)
            {
                string[] par=CourseIDsList[j].Split('#');
                if (par[0] == lbl_CourseID.Text)
                {
                    chk.Checked = true;
                    txt_Score.Text = par[1];
                }

                //string[] parameters = CourseIDsList[j].Split(',');
                //if (parameters.Length == 4)
                //{
                    //if (parameters[0] == lbl_CourseID.Text)
                    //{
                    //    chk.Checked = true;
                    //}
                //}
                //opcourse = obj.LoadVOTCourse(int.Parse(lbl_CourseID.Text));
                //lbl_Score.Text = opcourse.Score.ToString().Trim();
                //lbl_studyTime.Text = opcourse.CoursePeriod.ToString().Trim();
            }
        }
    }


    private bool CheckPageData()
    {
        string search = string.Format("CourseListName = '{0}'", txt_Name.Text.Trim());
        if (ViewState["CLID"] != null)
        {
            search += "and CourseList_Id<>" + ViewState["CLID"].ToString();
        }
        if (objCourseList.Count(search) != 0)
        {

            //lbl_msg_Error.Text = GetLanageValueByKey("lbl_msg_addjob_aspx_count", PowerCreator.LMS.Language.LanagetItemType.MSG);

            lbl_msg_Error.Text = "<br />" + GetLanageValueByKey("msg_AddNewType_checkMsg", PowerCreator.LMS.Language.LanagetItemType.Page);
            return false;
        }

        /*检查所有选择课程的总学时/学分与教学计划所设学时/学分的是否一致
        */

        ////获取当前选择教学计划的总学时/学分
        //int iPeriod_Plans = 0;
        //int iCredit_Plans = 0;
        //int iPlans_id = int.Parse(ddl_Plans.SelectedValue);
        //voPlans = objPlans.Load(iPlans_id);
        //iPeriod_Plans = voPlans.Period;
        //iCredit_Plans = voPlans.Credit;

        //获取当前批次的总学时/学分
        int iPeriod_Batch = int.Parse(lbl_Batch_xueshi.Text.Trim());
        int iCredit_Batch = int.Parse(lbl_Batch_xuefen.Text.Trim());


        //计算当前所有所选课程的总课时/学分
        int iPeriod_Sum = 0;
        int iCredit_Sum = 0;


        for (int i = 0; i < rptCourselist.Items.Count; i++)
        {
            CheckBox chk = (CheckBox)rptCourselist.Items[i].FindControl("chk_CourseID");
            //TextBox txt_Score = (TextBox)rptCourselist.Items[i].FindControl("txt_Score");
            //TextBox txt_studyTime = (TextBox)rptCourselist.Items[i].FindControl("txt_studyTime");
            Label lbl_Score = (Label)rptCourselist.Items[i].FindControl("lbl_Score");
            Label lbl_studyTime = (Label)rptCourselist.Items[i].FindControl("lbl_studyTime");
            TextBox txt_Score = (TextBox)rptCourselist.Items[i].FindControl("txt_Score");
            if (chk.Checked)
            {
                if (string.IsNullOrEmpty(lbl_Score.Text))
                {
                    iCredit_Sum += 0;
                }
                else
                {
                    iCredit_Sum += int.Parse(lbl_Score.Text.Trim());
                }

                if (string.IsNullOrEmpty(lbl_studyTime.Text))
                {
                    iPeriod_Sum += 0;
                }
                else
                {
                    iPeriod_Sum += int.Parse(lbl_studyTime.Text.Trim());
                }
                try
                {
                    int score;
                    score=int.Parse(txt_Score.Text);

                }
                catch (Exception ex)
                {
                    lbl_msg_Error.Text = "<br />所选课程通过百分比，必须填写且必须为整数，请检查！";
                    return false;
                }
                //try
                //{
                //    iPeriod_Sum += int.Parse(txt_studyTime.Text);

                //}
                //catch (Exception ex)
                //{
                //    lbl_msg_Error.Text = "<br />所选课程学时，必须填写且必须为整数，请检查！";
                //    return false;

                //}
            }
        }
        if (iCredit_Sum > iCredit_Batch)
        {
            lbl_msg_Error.Text = "<br />所选课程总学分不能超过此批次所设总学分，请检查！";
            return false;
        }
        if (iPeriod_Sum > iPeriod_Batch)
        {
            lbl_msg_Error.Text = "<br />所选课程总学时不能超过此批次所设总学时，请检查！";
            return false;
        }
        //try
        //{
        //    int iPlans = int.Parse(ddl_Plans.SelectedValue);

        //}
        //catch (Exception ex)
        //{
        //    lbl_msg_Error.Text = "<br />所属教学计划必须要选择，如果现在没有可以选择的，请先添加！";
        //    return false;
        //}
        return true;
    }
    protected void btnSave_Click(object sender, EventArgs e)
    {
        if (!CheckPageData()) return;
        VOTOCourseList vo = new VOTOCourseList();
        if (ViewState["CLID"] == null)
        {
            vo.CourseListName = txt_Name.Text.Trim();
            vo.Description = txt_Introduce.Text.Trim();
            vo.Batch_Id = int.Parse(ddl_Batch.SelectedValue);
            //vo.Plans_Id = int.Parse(ddl_Plans.SelectedValue);
            vo.CourseType_Id = int.Parse(ddl_CourseType.SelectedValue);
            vo.Xueke = int.Parse(ddl_XueKe.SelectedValue);
            vo.Xueduan = int.Parse(ddl_XueDuan.SelectedValue);
            if (radbtn_Yes.Checked == true)
                vo.Used = true;
            if (radbtn_No.Checked == true)
                vo.Used = false;
            string CourseIDlist = getCourseIDlist();
            vo.CourseID = CourseIDlist;
            vo.Createtime=DateTime.Now;

             PowerCreator.LMS.Web.Base.BasePage bpage=new PowerCreator.LMS.Web.Base.BasePage();
             vo.CreatorID = bpage.GetCreatedByInfo().UserID;
            if (objCourseList.Add(vo))
            {
                string url = "ManageCourseList.aspx";
                Script.RedirectURL(url);
            }
        }
        else
        {
            vo = objCourseList.Load(int.Parse(ViewState["CLID"].ToString()));
            vo.CourseListName = txt_Name.Text.Trim();
            vo.Description = txt_Introduce.Text.Trim();
            vo.Batch_Id = int.Parse(ddl_Batch.SelectedValue);
            //vo.Plans_Id = int.Parse(ddl_Plans.SelectedValue);
            vo.CourseType_Id = int.Parse(ddl_CourseType.SelectedValue);
            vo.Xueke = int.Parse(ddl_XueKe.SelectedValue);
            vo.Xueduan = int.Parse(ddl_XueDuan.SelectedValue);
            if (radbtn_Yes.Checked == true)
                vo.Used = true;
            if (radbtn_No.Checked == true)
                vo.Used = false;
            string CourseIDlist = getCourseIDlist();
            //Response.Write(CourseIDlist);
            //Response.End();
            vo.CourseID = CourseIDlist;
            vo.Createtime = DateTime.Now;

            PowerCreator.LMS.Web.Base.BasePage bpage = new PowerCreator.LMS.Web.Base.BasePage();
            vo.CreatorID = bpage.GetCreatedByInfo().UserID;
            if (objCourseList.Update(vo))
            {
                string url = "ManageCourseList.aspx";
                Script.RedirectURL(url);
            }
        }
    }
    protected void btnReset_Click(object sender, EventArgs e)
    {
        string url = "ManageCourseList.aspx";
        Script.RedirectURL(url);
    }


    protected void ddl_CourseType_SelectedIndexChanged(object sender, EventArgs e)
    {
        BinderSchoolStage(int.Parse(ddl_CourseType.SelectedValue));
        BinderCatalog(int.Parse(ddl_CourseType.SelectedValue));
        BinderRpt(int.Parse(ddl_CourseType.SelectedValue));
    }
    protected void ddl_Batch_SelectedIndexChanged(object sender, EventArgs e)
    {
        //BinderPlan(int.Parse(ddl_Batch.SelectedValue));
        BinderBatch_shifen(int.Parse(ddl_Batch.SelectedValue));

    }
    //protected void ddl_XueKe_SelectedIndexChanged(object sender, EventArgs e)
    //{
    //    BinderCheckBoxList(int.Parse(ddl_XueDuan.SelectedValue), int.Parse(ddl_XueKe.SelectedValue));
    //}

}